package cool.gaogao.jdbcTemplate.book.dao;

import cool.gaogao.jdbcTemplate.book.bean.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 高山青
 * @Date 2021/12/31  -   16:06
 */
@Repository
public class BookDaoImp implements BookDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public int addBook(Book book){
		String sql="insert into book values(?,?,?)";
		Object[] args={book.getBookid(),book.getBookname(),book.getBstatus()};
		return jdbcTemplate.update(sql,args);
	}

	@Override
	public int updateBook(Book book) {
		String sql="update book set bookname=?,bstatus=? where bookid=?";
		Object[] args={book.getBookname(),book.getBstatus(), book.getBookid()};
		return jdbcTemplate.update(sql,args);
	}

	@Override
	public int deleteBook(Book book) {
		String sql="delete from book where bookid=?";
		Object[] args={ book.getBookid()};
		return jdbcTemplate.update(sql,args);
	}

	@Override
	public int getCount() {
		String sql="select count(*) from book";
		return jdbcTemplate.queryForObject(sql,Integer.class);
	}

	@Override
	public Book findBook(Book book) {
		String sql="select * from book where bookid=?";
		Object[] args={ book.getBookid()};
		return jdbcTemplate.queryForObject(sql,args,new BeanPropertyRowMapper<Book>(Book.class));
	}

	@Override
	public List<Book> findAll() {
		String sql="select * from book";
		return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
	}

	@Override
	public int[] bathAddBook(List<Object[]> books) {
		String sql="insert into book values(?,?,?)";
		return jdbcTemplate.batchUpdate(sql,books);
	}

	@Override
	public int[] bathUpdateBook(List<Object[]> books) {

		String sql="update book set bookname=?,bstatus=? where bookid=?";
		return jdbcTemplate.batchUpdate(sql,books);
	}

	@Override
	public int[] bathDeleteBook(List<Object[]> books) {

		String sql="delete from book where bookid=?";
		return jdbcTemplate.batchUpdate(sql,books);
	}
}
